#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;



bool myfunction (const vector<double>& i, const vector<double>& j) 
{ 
  return (i[0]<j[0]); 
}


void main()
{
  ifstream in("QN_final_values_t0_2terms.out");
  vector<vector<double> > allData;

  if ( !in.is_open() )
  {
    cout << "can't open file\n";
    exit(1);
  }

  string aLine;
  int line = 0;
  while ( getline(in, aLine, '\n') ) 
  {
    line++;

    stringstream str(aLine);
    double value;

    vector<double> vLine;
    while ( str >> value )
    {
      vLine.push_back(value);
    }
    allData.push_back(vLine);   
  }

  sort (allData.begin(), allData.end(), myfunction);

  ofstream out("startingConfigurations.txt");
  ofstream outCount("count.txt");

  int count = 0;
  double previousFOM = 0.0;
  for (unsigned int i = 0; i < allData.size(); i++)
  {


    if ( previousFOM == allData[i][0])
    {
      count++;
      if ( count < 6 )
      {
        out.precision(5);
        out << setw(17) << allData[i][0];
        out.precision(9);
        for (unsigned int j = 1; j < allData[i].size(); j++)
          out << setw(17) << allData[i][j];
        out << "\n";
      }
    }
    else
    {
      outCount << previousFOM << " " << count << endl;
      count = 1;
        out.precision(5);
        out << setw(17) << allData[i][0];
        out.precision(9);
        for (unsigned int j = 1; j < allData[i].size(); j++)
          out << setw(17) << allData[i][j];
        out << "\n";
      previousFOM = allData[i][0];
    }
  }
}
